<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-input
            v-model="search"
            placeholder="输入学生姓名搜索"
            clearable @change="getStudentList">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getStudentList">搜索</el-button>
      </el-form-item>

      <el-form-item>
        <el-popconfirm title="确定批量删除?" @confirm="delHandle(null)">
          <el-button type="danger" slot="reference" :disabled="delBtlStatu" v-if="hasPermission()">批量删除</el-button>
        </el-popconfirm>
      </el-form-item>
      <el-form-item>
        <template>
          <el-select v-model="student_class" clearable placeholder="班级分类" @change="getStudentList">
            <el-option
                v-for="item in class_list"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
    </el-form>

    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="username"
          label="学生姓名"
          width="140">
      </el-table-column>
      <el-table-column
          prop="class_name"
          label="班级"
          width="160">
      </el-table-column>
      <el-table-column
          prop="mobile"
          label="手机号"
          width="150">
      </el-table-column>
      <el-table-column
          prop="email"
          label="邮箱"
          width="220">
      </el-table-column>
      <el-table-column
          prop="join_time"
          label="加入时间"
          width="170">
      </el-table-column>
      <el-table-column
          prop="paper_count"
          label="考试次数"
          width="95">
      </el-table-column>
      <el-table-column
          prop="is_active"
          label="激活状态"
          width="85">
        <template slot-scope="scope">
          <el-button type="success" round size="mini" v-if="!scope.row.is_active" @click="goActivate(scope.row.id)">激活
          </el-button>
          <el-button type="danger" round size="mini" v-else @click="goActivate(scope.row.id)">锁定</el-button>
        </template>
      </el-table-column>

      <el-table-column
          prop="icon"
          label="操作">
        <template slot-scope="scope">
          <el-button @click="editHandle(scope.row)" size="mini">编辑</el-button>
          <span>&nbsp;&nbsp;</span>
          <template>
            <el-popconfirm title="确定删除?" @confirm="delHandle(scope.row.id)">
              <el-button type="danger" size="mini" slot="reference" v-if="hasPermission()">删除</el-button>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 50, 100]"
        :current-page="current"
        :page-size="size"
        :total="total">
    </el-pagination>
    <!--新增对话框-->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">
      <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-editForm">
        <el-form-item label="学生姓名" prop="username" label-width="100px">
          <el-input v-model="editForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="所属班级" prop="class_name" label-width="100px">
          <template>
            <el-select v-model="editForm.student_class" placeholder="请选择班级" style="width: 460px">
              <el-option
                  v-for="item in class_list"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile" label-width="100px">
          <el-input v-model="editForm.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email" label-width="100px">
          <el-input v-model="editForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交修改</el-button>
          <el-button @click="resetForm">重置表单</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <div style="text-align: center; padding-bottom: 10px;">&COPY;copyleft@海饼干</div>
  </div>
</template>

<script>
export default {
  name: "StudentList",
  data() {
    return {
      search: '',
      delBtlStatu: true,
      total: 0,
      size: 10,
      current: 1,
      dialogVisible: false,
      editForm: {
        class_id: '',
      },
      class_list: [],
      student_class: '',
      tableData: [],
      editFormRules: {
        username: [
          {required: true, message: '请输入学生姓名', trigger: 'blur'}
        ],
        mobile: [
          {required: true, message: '请输入学生电话号码', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入学生邮箱', trigger: 'blur'}
        ]
      },
      multipleSelection: [],
      permDialogVisible: false,
      permForm: {},
      permTreeData: []
    }
  },
  created() {
    this.getStudentList()
  },
  methods: {
    goActivate(id) {
      this.$http({
        url: this.$settings.BASE_URL + '/user/edit_student/activate/',
        method: 'post',
        data: {student_id: id},
      }).then(res => {
        if (res.data.code === 20000) {
          this.getStudentList()
        } else {
          this.$message({
            type: 'warning',
            message: res.data.msg,
            duration: 3000,
          })
        }
      })
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.delBtlStatu = val.length === 0
    },
    handleSizeChange(val) {
      this.size = val
      this.getStudentList()
    },
    handleCurrentChange(val) {
      this.current = val
      this.getStudentList()
    },
    resetForm() {
      this.dialogVisible = false
      this.editForm = {}
    },
    handleClose() {
      this.resetForm()
    },
    getStudentList() {
      this.$http({
        url: this.$settings.BASE_URL + '/user/student_list/',
        method: 'get',
        params: {
          page: this.current,
          size: this.size,
          search: this.search,
          student_class: this.student_class,
        },
      }).then(res => {
        if (res.data.code === 20000) {
          this.tableData = res.data.result.results
          console.log(this.tableData)
          this.total = res.data.result.count
        } else {
          this.$message({
            type: 'warning',
            message: res.data.msg,
            duration: 3000,
          })
        }
      })
      this.$http({
        url: this.$settings.BASE_URL + '/user/student_class/',
        method: 'get',
      }).then(res => {
        this.class_list = res.data.result.results
      })
    },
    submitForm() {
      this.$http({
        url: this.$settings.BASE_URL + '/user/edit_student/update_student/',
        method: 'post',
        data: this.editForm,
      }).then(res => {
        if (res.data.code === 20000) {
          this.getStudentList()
          this.$message({
            type: 'success',
            message: res.data.msg,
            duration: 3000,
          })
          this.dialogVisible = false
        } else {
          this.$message({
            type: 'error',
            message: '请检查邮箱格式,或用户名重复',
            duration: 3000,
          })
        }
      })
    },
    editHandle(row) {
      this.editForm = row
      this.dialogVisible = true
    },
    delHandle(id) {
      let ids = []
      if (id) {
        ids.push(id)
      } else {
        this.multipleSelection.forEach(row => {
          ids.push(row.id)
        })
      }
      this.$http({
        url: this.$settings.BASE_URL + '/user/edit_student/del_student/',
        method: 'post',
        data: ids,
      }).then(res => {
        if (res.data.code === 20000) {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: 'success',
          });
          this.getStudentList()
        } else {
          this.$message({
            type: 'error',
            message: res.data.msg,
            duration: 3000,
          })
        }
      })
    },
  }
}
</script>

<style scoped>

</style>
